<?php

class Antz_Form_Components_DateSelect extends Antz_Form_Component
{
    protected $options = array();
    protected $type = 'DateSelect';

    public function __construct($name, $label, $value='', $yearStart='', $yearEnd=''){
        $this->id = 'sel'.rand();
        $this->name = $name;
        $this->label = $label;
        $this->yearStart = $yearStart;
        $this->yearEnd = $yearEnd;

        if($this->yearStart=='') $this->yearStart = date('Y', time()-86400*90);
        if($this->yearEnd=='') $this->yearEnd = date('Y', time()+86400*90);

        if($value=='') $value = array('month'=>date('n'), 'year'=>date('Y'), 'day'=>date('j'));

        $this->value = $value;
        $this->setLang('en');
    }

    public function setLang($lang='en'){
        $lang = 'en';
        switch($lang){
            case 'en':
                $this->months = array(
                    '1'=>'January',
                    '2'=>'February',
                    '3'=>'March',
                    '4'=>'April',
                    '5'=>'May',
                    '6'=>'June',
                    '7'=>'July',
                    '8'=>'August',
                    '9'=>'September',
                    '10'=>'October',
                    '11'=>'November',
                    '12'=>'December'
                );
            break;
        }
    }

    public function render($wrap=true, $label=true){
        // generate the month options
        $this->monthSel = Antz::newObject('Antz_Form_Components_DateSelect_Select', array($this->name.'Month', $this->value['month']));

        for($i=1; $i<=12; $i++){
            $this->monthSel->add($this->months[$i], $i);
        };

        // generate the day options
        $this->daySel = Antz::newObject('Antz_Form_Components_DateSelect_Select', array($this->name.'Day', $this->value['day']));
        for($i=1; $i<=31; $i++){
            $this->daySel->add($i);
        };
        // generate the year options
        $this->yearSel = Antz::newObject('Antz_Form_Components_DateSelect_Select', array($this->name.'Year', $this->value['year']));
        for($i=$this->yearStart; $i<=$this->yearEnd; $i++){
            $this->yearSel->add($i);
        };

		global $gCms;
     	$Antz = Antz::registry('Antz');
     	if($this->required) $this->aftertext = '<span class="required"><img src="'.$gCms->config['root_url'].'/modules/Antz/images/required.gif" alt="'.$Antz->Lang('required').'" /></span>'.$this->aftertext;


        $returnString = $this->monthSel->render().$this->daySel->render().$this->yearSel->render();
        if($this->showLabel) $returnString = '<label>'.$this->label.'</label>'.$returnString.'<div class="aftertext">'.$this->aftertext.'</div>';
        if($this->wrap) return $this->wrap($returnString);
        else return $returnString;
    }
}
